package a2;

/**
 * Item is an abstract class that represents an entity on a virtual
 * {@link FileSystem}. It is the parent class of both {@link File} and
 * {@link Directory}. It has a {@link String} name and a parent
 * {@link Directory} that contains this Item.
 * 
 * @author Dylan Seago, Tianran Liu, Hilal Dib, Paschal Oduoza
 */
abstract class Item {

	/**
	 * Name of this {@link Item}. Includes the file type (eg .txt)
	 */
	private String name;
	/**
	 * {@link Directory} Object that contains this {@link Item}.
	 */
	private Directory parent;

	/**
	 * Initialize a new {@link Item} with a given {@code name}, and
	 * {@code parent} {@link Directory}.
	 * 
	 * @param name
	 *            {@link String} name of this {@link Item}.
	 * @param parent
	 *            {@link Directory} that contains this {@link Item}.
	 */
	public Item(String name, Directory parent) {
		this.name = name;
		this.parent = parent;
	}

	/**
	 * Gets the name of this {@link Item}.
	 * 
	 * @return The {@link String} name of this {@link Item}.
	 */
	public String getName() {
		if(name == ""){
			return "Root";
		}
		return name;
	}

	/**
	 * Sets the name of this {@link Item}.
	 * 
	 * @param name
	 *            The {@link String} name to set this {@link Item} to.
	 * @return This {@link Item}.
	 */
	public Item setName(String name) {
		this.name = name;
		return this;
	}
	
	/**
	 * Gets the absolute path name of this {@link Item} starting from the
	 * root.
	 * 
	 * @return Path name of this {@link Directory}.
	 */
	public String getPath() {
		if (getParent() != null) {
			return getParent().getPath() + this.getName() + "/";
		}
		return "/";
	}

	/**
	 * Gets the parent {@link Directory} that contains this {@link Item}.
	 * 
	 * @return The parent {@link Directory} that contains this {@link Item}.
	 */
	public Directory getParent() {
		return parent;
	}

	/**
	 * Prints out the path of this {@link Item}.
	 */
	public String ls(){
		return getPath();
	}

}
